package edu.scau.mis.sys.expression;

import edu.scau.mis.sys.security.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;

/**
 * 自定义权限注解
 */
@Component("ex")
public class SGExpressionRoot {

    public boolean hasAuthority(String... authorities){

        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Collection<? extends GrantedAuthority> grantedAuthorities =  loginUser.getAuthorities();

        //判断是否有权限
        for(String authority : authorities){
            GrantedAuthority grantedAuthority  = new SimpleGrantedAuthority(authority);
            //判断用户权限集合中是否存在authority
            if(grantedAuthorities.contains(grantedAuthority)){
                return true;
            }
        }

        return false;
    }


}
